package edu.usc.libtalk;

import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import edu.usc.libtalk.task.GetJSONArrayTask;
import edu.usc.libtalk.utils.ApplicationService;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
import edu.usc.libtalk.utils.InternalConstant;

public class OnLineUsersActivity extends Activity implements InternalConstant {
	private String library;
	private String username;
	private String deviceID;
	private int user_level;
	private int user_seat;	
	private static final String TAG = "ChatsActivity";
	private JSONArray messages;
	
	private ArrayList onlineUsersList;
	private ArrayList onlineUsersLevel;
	private ArrayList onlineUsersSeat;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chats);
        
        username = ApplicationService.getUserName();
        library = ApplicationService.getLibrary();
        deviceID = ApplicationService.getDeviceID();
        user_level = ApplicationService.getLevel();
        user_seat = ApplicationService.getSeat();        
        
        updateOnlineUsers();
    }
    
    @Override
    public void onResume() {
    	super.onResume();
    	updateOnlineUsers();
    }
    
    @Override
    public void onPause() {
	     super.onPause();
    }    
    
    public void updateOnlineUsers()
    {
	    String url = HOSTNAME + GET_ALL_ONLINE_USERS_URL + "?library=" + library;       	    	    
	    new GetJSONArrayTask()
	    {
			@Override
			protected void onPostExecute(JSONArray result)
			{
				if(result == null)
				{
					ApplicationService.inform(getString(R.string.nointernet));
					finish();
					return;
				}         		
				
				messages = result;
				
				// Parse JSON Object				
	            try
	            {
	            	onlineUsersList = new ArrayList();
	            	onlineUsersLevel = new ArrayList();
	            	onlineUsersSeat  = new ArrayList();	            	
            	
	            	for(int i=0; i<messages.length(); i++) 
	            	{
	            		
	            		JSONObject json_data = messages.getJSONObject(i);
	            		onlineUsersList.add("Level: " + json_data.getString("level") +", Seat: "+ json_data.getString("seat") + " (" + json_data.getString("interest") + ")");
	            		onlineUsersLevel.add(json_data.getString("level"));
	            		onlineUsersSeat.add(json_data.getString("seat"));

	            	}
	            	
	            	ArrayAdapter adapter;
	            	adapter = new ArrayAdapter(OnLineUsersActivity.this, android.R.layout.simple_list_item_1, onlineUsersList);
	            	
	            	ListView listView =(ListView)findViewById(R.id.chat_history_listview);
	            	listView.setAdapter(adapter);
	                
	            	listView.setOnItemClickListener(new OnItemClickListener() {
	                     public void onItemClick(AdapterView parent, View view, int position, long id)
	                     {
	                    	 Log.e(TAG, onlineUsersList.get(position).toString());
	                    	 
	                    	 Intent intent = new Intent(OnLineUsersActivity.this, MessageBoardActivity.class);
	                    	 intent.putExtra(SELECTED_LEVEL, onlineUsersLevel.get(position).toString());
	                    	 intent.putExtra(SELECTED_SEAT, onlineUsersSeat.get(position).toString());
	                    	 startActivity(intent);
	                     }
	                 });
	            	
	            }
	            catch(JSONException e){
	            	Log.e(TAG, "Error parsing data "+e.toString());
	            }         		
	     	}	
	    }.execute(url.replaceAll(" ", "%20"));	        
    }
    
    @Override
    public void onBackPressed()
    {
		AlertDialog.Builder alertDialog = new AlertDialog.Builder(OnLineUsersActivity.this);
		alertDialog.setTitle("Exit this application");
		alertDialog.setMessage("Are you sure you want to close this application?");
		
		alertDialog.setPositiveButton("Confirm", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface arg0, int arg1) {
				
				// Go to home screen
				Intent intent = new Intent(Intent.ACTION_MAIN);
				intent.addCategory(Intent.CATEGORY_HOME);
				intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				startActivity(intent);	
			}});
		
		alertDialog.setNegativeButton("Cancel", null);		
		
		alertDialog.show();
    }
}



